package servlet;

import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import Database.AuditLogDB;
import Database.CommitteeDB;
import Model.Committee;

/**
 * Servlet implementation class StaffappChangeStatus
 */
public class CommitteeChangeStatus extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CommitteeChangeStatus() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		javax.servlet.http.HttpSession session = request.getSession();
		String approvedBy = (String) session.getAttribute("username");

		String id;

		if (request.getParameterValues("committeeID") != null) {

			String[] committeeIDList = request.getParameterValues("committeeID");
			Committee com = new Committee();
			if(request.getParameter("action").equals("Remove"))
			{
				com.removeCommittees(committeeIDList);
				AuditLogDB.insertLog("Delete committee",
						(String) request.getSession().getAttribute("name"));
			}
			for (int i = 0; i < committeeIDList.length; i++) {
				id = committeeIDList[i];
				System.out.println("ID in CommitteeChangeStatus: " + id);
				Committee app = new Committee();
				if (request.getParameter("action").equals("Approve")) 
				{
					// Approve
					app.setId(id);
					app.setApprovedBy(approvedBy);
					//	Status code of 2 is approved
					app.setStatus(2);
					CommitteeDB.updateStatus(app);
					AuditLogDB.insertLog("approve committee", (String) request
							.getSession().getAttribute("name"));
				} //else if (request.getParameter("action").equals("Remove")) {
//					// Remove
//					app.setId(id);
//					app.setStatus(3);
//				//	AuditLogDB.insertLog("Delete committee", (String) request
//			//				.getSession().getAttribute("name"));
//				} 
			}
			if (request.getParameter("action").equals("Approve")) {
				response.sendRedirect("committee_approve.jsp?error=2&action=approved");
			} else if (request.getParameter("action").equals("Remove")) {
				response.sendRedirect("committee_approve.jsp?error=2&action=rejected");
			} 
		} else {
			response.sendRedirect("committee_approve.jsp?error=1");
		}

	}

}
